package com.example.lostandfoundsystem.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author lucifer
 * @since 2024-07-12-15:11
 */
public class DatabaseUtil implements ApplicationContextAware {
    protected static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (DatabaseUtil.applicationContext == null) {
            DatabaseUtil.applicationContext = applicationContext;
        }
    }

    public static Object getBean(String name) {
        // name表示其他要注入的注解name名
        return applicationContext.getBean(name);
    }

    // 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}
